package com.lm.survey.model;

import java.util.*;

public class QuestionGroup {

    private int id;
    private int questonnareId;
    private String name;

    private String description;
    int seq = 0;
    String code;
    int randomDisplay;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public int getRandomDisplay() {
        return randomDisplay;
    }

    public void setRandomDisplay(int randomDisplay) {
        this.randomDisplay = randomDisplay;
    }

    Map<Integer, QuestionItem> maps = new HashMap<Integer, QuestionItem>();

    public QuestionItem getQuestionItem(int itemId) {
        return maps.get(itemId);
    }

    public List<QuestionItem> getItems() {
        if(this.randomDisplay==0){
            TreeSet<QuestionItem> set = new TreeSet<QuestionItem>(
                    new ItemComparator());
            set.addAll(maps.values());
            return new ArrayList(set);
        }else{
            TreeSet<QuestionItem> set = new TreeSet<QuestionItem>(
                    new RandomItemComparator());
            set.addAll(maps.values());
          
            ArrayList list =  new ArrayList(set);
            return list;
            
        }
      
        // return new ArrayList(maps.values());
    }
    
    private List<QuestionItem> getRandomItems(){
        return new ArrayList(maps.values());
        
        
        
    }

    public void setItems(List<QuestionItem> items) {
        QuestionItem item = null;
        for (int i = 0; i < items.size(); i++) {
            item = items.get(i);
            maps.put(item.getId(), item);
        }

    }

    public int getSeq() {
        return seq;
    }

    public void setSeq(int seq) {
        this.seq = seq;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getQuestonnareId() {
        return questonnareId;
    }

    public void setQuestonnareId(int questonnareId) {
        this.questonnareId = questonnareId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    class ItemComparator implements Comparator {
        public int compare(Object o1, Object o2) {
            QuestionItem i1 = (QuestionItem) o1;
            QuestionItem i2 = (QuestionItem) o2;
            return i1.compare(i2);

        }
    }
    
    class RandomItemComparator implements Comparator {
        
        public int compare(Object o1, Object o2) {
          
            int i = new Random().nextInt(1);
            if(i==1) return -1;
            else return 1;
            

        }
    }
}
